package com.smart.smartdata.ble;

import android.content.Context;
import android.util.Log;

import com.inuker.bluetooth.library.BluetoothClient;
import com.inuker.bluetooth.library.Constants;
import com.inuker.bluetooth.library.connect.listener.BleConnectStatusListener;
import com.inuker.bluetooth.library.connect.response.BleNotifyResponse;
import com.inuker.bluetooth.library.connect.response.BleReadResponse;
import com.inuker.bluetooth.library.connect.response.BleUnnotifyResponse;
import com.inuker.bluetooth.library.connect.response.BleWriteResponse;

import java.util.UUID;

public class TemperatureBleManager {
    private String TAG = "TemperatureBleManager";
    private static TemperatureBleManager mBleManager;
    private Context mContext;
    private UUID serviceUUID = UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb");
    private  UUID UUID_WRITE_CHARACTERISTIC = UUID.fromString("0000fff1-0000-1000-8000-00805f9b34fb");
    private UUID UUID_NOTIFICATION_CHARACTERISTIC =
            UUID.fromString("000ffe2-0000-1000-8000-00805f9b34fb");
    private Boolean mBleConnected;
    private BleDevice mConnectedDevice;

    private TemperatureBleManager() {}

    public static TemperatureBleManager getInstance() {
        if (mBleManager == null) {
            synchronized (TemperatureBleManager.class) {
                if (mBleManager == null) {
                    mBleManager = new TemperatureBleManager();
                }
            }
        }
        return mBleManager;
    }


    public void init(Context context) {
        mContext = context;
    }

    public boolean isBleOpen() {
        return ClientManager.getClient().isBluetoothOpened();
    }

    public void stopSearch() {
        ClientManager.getClient().stopSearch();
    }

    public BluetoothClient getClient() {
        return ClientManager.getClient();
    }

    public void setConnected(Boolean connet) {
        mBleConnected = connet;
    }

    public boolean isConnected(){
        return mBleConnected;
    }

    public void closeBle() {
        ClientManager.getClient().closeBluetooth();
    }

    public void setBleDevice(BleDevice device) {
        this.mConnectedDevice = device;
    }

    public BleDevice getBleDevice() {
        return mConnectedDevice;
    }

    public void setConnectStatusListener() {
        BleConnectStatusListener mBleConnectStatusListener = new BleConnectStatusListener() {
            @Override
            public void onConnectStatusChanged(String mac, int status) {
                if (status == Constants.STATUS_CONNECTED) {
                    Log.i(
                            "ConnectStatusListener",
                            "STATUS_CONNECTED"
                    );
                    mBleConnected = true;
                } else if (status == Constants.STATUS_DISCONNECTED) {
                    Log.i(
                            "ConnectStatusListener",
                            "STATUS_DISCONNECTED"
                    );
                    mBleConnected = false;
                }
            }
        };

        ClientManager.getClient().registerConnectStatusListener(mConnectedDevice.getMac(),mBleConnectStatusListener);
    }


    /**
     * 读Characteristic
     */
    public void readCharacteristic(String mac, BleReadResponse response) {
        ClientManager.getClient().read(
                mac, serviceUUID, UUID_WRITE_CHARACTERISTIC,
                response
        );
    }

    /**
     *写Characteristic
     */
    public void writeCharacteristic(
            String mac,
            byte[] bytes ,
            BleWriteResponse response,
            BleNotifyResponse notifyResponse
    ) {
        ClientManager.getClient().write(mac, serviceUUID, UUID_WRITE_CHARACTERISTIC, bytes, response);
        if (notifyResponse != null){
            openNotify(mac,notifyResponse);
        }
    }

    public void writeCharacteristic(
            String mac,
            byte[] bytes,
            BleWriteResponse response
    ) {
        ClientManager.getClient().write(mac, serviceUUID, UUID_WRITE_CHARACTERISTIC, bytes, response);
    }


    public void writeCharateristicNoRsp(String mac, byte[] bytes, BleWriteResponse response) {
        ClientManager.getClient().writeNoRsp(
                mac, serviceUUID, UUID_WRITE_CHARACTERISTIC, bytes,
                response
        );
    }


    public void openNotify(String mac,BleNotifyResponse bleNotifyResponse) {
        ClientManager.getClient().notify(
                mac,
                serviceUUID,
                UUID_NOTIFICATION_CHARACTERISTIC,
                bleNotifyResponse);

    }

    public void closeNotify(String mac) {
        ClientManager.getClient().unnotify(mac, serviceUUID, UUID_NOTIFICATION_CHARACTERISTIC,
                new BleUnnotifyResponse() {
                    @Override
                    public void onResponse(int code) {
                        if (code == 0) {

                        }
                    }
                }
             );
    }
}
